package com.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.beans.Book;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beans.Chapter;
import com.mapper.ChapterMapper;
import com.service.ChapterService;
import org.springframework.util.StringUtils;

@Service
@Transactional
public class ChapterServiceImpl implements ChapterService {
    @Resource
    private ChapterMapper chapterMapper;

    public List<Chapter> getChapter(String novelname) {
        return chapterMapper.getChapter(novelname);
    }

    public int addChapter(Chapter Chapter) {
        return chapterMapper.addChapter(Chapter);
    }

    public int deleteChapter(int id) {
        return chapterMapper.deleteChapter(id);
    }

    public Chapter getChapterId(int chapterid, String novelname) {
        return chapterMapper.getChapterId(chapterid, novelname);
    }

    public List<Chapter> getAllChapter() {
        return chapterMapper.getAllChapter();
    }

    public Integer updateChapter(Chapter Chapter) {
        return chapterMapper.updateChapter(Chapter);
    }

    public Chapter getChapterById(int id) {
        return chapterMapper.getChapterById(id);
    }

    @Override
    public Map<String, Object> queryByPage(Chapter chapter, Integer page, Integer limit) {
        Map<String, Object> resMap = new HashMap<>(4);
        PageHelper.offsetPage((page - 1) * limit, limit);
        List<Chapter> dataList = Collections.emptyList();
        if (StringUtils.hasText(chapter.getNovelname())) {
            dataList = chapterMapper.getChapter(chapter.getNovelname());
        } else {
            dataList = chapterMapper.getAllChapter();
        }
        PageInfo<Chapter> pageInfo = new PageInfo<>(dataList);
        resMap.put("code", 0);
        resMap.put("msg", "查询成功");
        resMap.put("data", pageInfo.getList());
        resMap.put("count", pageInfo.getTotal());
        return resMap;
    }
}


